package com.roy.giftools;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.roy.giftools.utils.ImageConverter;

import java.io.File;
import java.util.ArrayList;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

public class Pic2GifActivity extends AppCompatActivity implements View.OnClickListener {

    private static boolean SAVE_STATE = FALSE;
    private ImageView iv;
    public TextView delayTV;
    public TextView compressTV;

    public int delay = 200, compress = 5;
    private String gifPath;
    private ArrayList<String> pathList;

    public ImageConverter imageConverter = new ImageConverter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic2gif);
        setTitle("保存GIF");

        //读取默认配置生成的GIF路径
        Bundle bundle = getIntent().getExtras();
        gifPath = bundle.getString("gifPath");
        pathList = bundle.getStringArrayList("pathList");
        Log.i("Tag", gifPath);
        Log.i("Tag", String.valueOf(pathList));
        //显示默认配置生成的GIF
        iv = (ImageView) findViewById(R.id.imageView);
        Glide.with(this).load(gifPath).asGif().into(iv);

        SAVE_STATE = FALSE;

        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        imageConverter.clearTmpDir();
        if (!SAVE_STATE) {
            File file = new File(gifPath);
            file.delete();
        }
    }

    private void initView() {

        compressTV = (TextView) findViewById(R.id.compressNum);
        SeekBar delaySB = (SeekBar) findViewById(R.id.delaySeekBar);
        SeekBar compressSB = (SeekBar) findViewById(R.id.compressSeekBar);
        Button applyBN = (Button) findViewById(R.id.apply);
        Button saveBN = (Button) findViewById(R.id.save);

        applyBN.setOnClickListener(this);
        saveBN.setOnClickListener(this);
        delaySB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                delay = progress;
                delayTV.setText(String.valueOf(delay));
                if (progress == 0) {
                    delay = 1;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        compressSB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                compress = progress;
                compressTV.setText(String.valueOf(compress));
                if (progress == 0) {
                    compress = 1;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.apply:
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        imageConverter.clearTmpDir();
                        Log.i("Tag", "Temp Dir has been clear.");
                        String gifName = imageConverter.stringToMD5(pathList.get(0));
                        gifPath = imageConverter.pic2gif(pathList, gifName, delay, compress);
                    }
                });
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.i("Tag", "Main Thread is back to work.");
                Glide.with(Pic2GifActivity.this).load(gifPath).asGif().into(iv);
                break;
            case R.id.save:
                Toast.makeText(this, "已保存至 " + gifPath, Toast.LENGTH_SHORT).show();
                SAVE_STATE = TRUE;
                break;
        }
    }
}
